package org.jiucai.app.demo.listener;

import javax.servlet.ServletRequestEvent;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.jiucai.appframework.base.listener.AppLogListener;
import org.jiucai.cas.auth.util.UserUtil;
import org.jiucai.cas.domain.UserInfo;

/**
 * 用户日志记录
 * @author zhaidw
 *
 */
public class UserLogListener extends AppLogListener {

	public UserLogListener() {
		super();
		log.info("UserLogListener inited.");
	}

	@Override
	public void requestInitialized(ServletRequestEvent event) {

		super.requestInitialized(event);

		HttpServletRequest request = (HttpServletRequest) event
				.getServletRequest();

		HttpSession session = request.getSession(true);
		String sessionId = session.getId();

		UserInfo user = UserUtil.getUser();
		if (null != user) {
			String userName = user.getLoginName();
			adapter.put("sid", userName);
		} else {

			adapter.put("sid", sessionId);
		}
	}

	@Override
	public void requestDestroyed(ServletRequestEvent event) {

		super.requestDestroyed(event);

		adapter.remove("sid");

	}

}
